var chartDom_w = document.getElementById("wave");
var myChart_w = echarts.init(chartDom_w);
var option_w;
//当前选择 周 或者 月
var choiceWaveDate;
//防止刷新
let waveDate = window.sessionStorage.getItem("waveDate");
if (waveDate == "周") {
  choiceWaveDate = 0;
  $("#wave_content").text("周");
} else if (waveDate == "月") {
  choiceWaveDate = 1;
  $("#wave_content").text("月");
} else {
  choiceWaveDate = 0;
}
//请求
function getwave() {
  $.ajax({
    url: baseUrl + "/faultLog",
    type: "POST",
    dataType: "json",
    data: {
      type: choiceWaveDate, //1本月 0本周
    },
    success: function (data) {
      waveData(data.data);
    },
    // 超时时间
    timeout: 4000,
    // 失败的回调
    error: function () {
      alert("设备故障情况请求数据失败！");
    },
  });
}
//获取数据
function waveData(res) {
  let wave_x = res.map((item) => {
    //x轴
    return item.data_date ? item.data_date : item.date;
  });
  if (choiceWaveDate == 0) {
    var wave_y = res.map((item) => {
      //y轴
      return item.total;
    });
  } else {
    var wave_y = res.map((item) => {
      //y轴
      return item.num;
    });
  }
  waveEcharts(wave_x, wave_y);
}

//echarts 数据渲染
function waveEcharts(wave_x, wave_y) {
  option_w = {
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "none",
        label: {
          backgroundColor: "#6a7985",
        },
      },
    },
    grid: {
      left: "5%",
      top: "10%",
      bottom: "15%",

      right: "5%",
    },
    xAxis: [
      {
        type: "category",
        boundaryGap: false,
        data: wave_x,
        axisTick: {
          show: false,
        },
        axisLabel: {
          color: "#3C9BC4",
        },
      },
    ],
    yAxis: [
      {
        type: "value",
        splitLine: {
          lineStyle: {
            type: "dashed", //虚线
            opacity: 0.2,
          },
        },
      },
    ],
    series: [
      {
        smooth: true, //变为曲线 默认false折线
        name: "设备故障数量",
        type: "line",
        stack: "Total",
        emphasis: {
          focus: "series",
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "#0FC4C2",
            },
            {
              offset: 1,
              color: "rgb(247, 186, 30, 0.1)",
            },
          ]),
        },
        data: wave_y,
      },
    ],
  };
  myChart_w.setOption(option_w);
}

/* 选择周或者月 */
$(".wave_week li").each(function () {
  $(this).click(function () {
    $("#wave_content").text($(this).text());
    window.sessionStorage.setItem("waveDate", $(this).text());
    if ($(this).text() == "周") {
      choiceWaveDate = 0;
      getwave();
    } else {
      choiceWaveDate = 1;
      getwave();
    }
  });
});
/* 刷新数据 */
function refreshData_w(ok_data) {
  //刷新数据
  var option = myChart_w.getOption();
  option.series[0].data = ok_data.y_data;
  option.xAxis = {
    type: "category",
    boundaryGap: false,
    data: ok_data.x_data,
    axisTick: {
      show: false,
    },
    axisLabel: {
      color: "#3C9BC4",
    },
  };
  myChart_w.setOption(option, true);
}

window.addEventListener("resize", function () {
  myChart_w.resize();
});
